package com.l.web.master.webcontent;

import com.l.web.WebConstant;
import org.apache.log4j.Logger;
import org.l.qiao.*;
import org.l.qiao.annotation.Privilege;
import org.l.qiao.builder.Field;
import org.l.qiao.builder.Sort;
import org.l.qiao.util.ClusterUtil;
import com.l.web.util.ControllerSupport;
import com.l.web.common.ResponseInfo;
import org.l.qiao.web.form.Form;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(WebContentController.URI)
public class WebContentController extends ControllerSupport {

    public static final String URI = WebConstant.MASTER_URI + "/webContent";
    private static final Logger LOGGER = Logger.getLogger(WebContentController.class);
    @Autowired
    private WebContentService webContentService;

//    public String execute() {
//        ServiceSupport service;
//
//        switch (act) {
//            case "add":
//                service = getService("WebContentAddService");
//                break;
//            case LIST:
//                service = getService("WebContentListService");
//                break;
//            case REMOVE:
//                service = getService("WebContentRemoveService");
//                break;
//            case DETAIL:
//                service = getService("WebContentDetailService");
//                break;
//            default:
//                service = getService("WebContentService");
//        }
//
//        String result = service.execute();
//        return result;
//    }

    @Privilege(module = "栏目", function = "栏目内容", operation = "查询")
    @RequestMapping("index.do")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        List<Cluster> webNodes = webContentService.getWebNodes();
        modelMap.put("webNodes", webNodes);
        return new ModelAndView(URI + "/index", modelMap);
    }

    @Privilege(module = "栏目", function = "栏目内容", operation = "查询")
    @RequestMapping("listForm.do")
    public ModelAndView listForm(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        String webNodeId = request.getParameter("webNodeId");
        modelMap.put("webNodeId", webNodeId);
        return new ModelAndView(URI + "/listForm", modelMap);
    }

    @Privilege(module = "栏目", function = "栏目内容", operation = "查询")
    @RequestMapping("list.do")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        Form form = getForm(request);
        Page page = form.getPage();
        WebContent webContent = new WebContent();
        boolean result = form.getCluster(webContent);
        webContent.initWebNodeId();

        Field field = new Field(WebContent.TITLE, WebContent.WEB_NODE_ID, WebContent.CREATED);

        Sort sort = new Sort();
        sort.desc(WebContent.CREATED);

        List<Cluster> list = webContentService.getPageList(page, webContent, field, sort);

        modelMap.put("Cluster", list);
        modelMap.put("page", page);
        return new ModelAndView(URI + "/list", modelMap);
    }

    @Privilege(module = "栏目", function = "栏目内容", operation = "详情")
    @RequestMapping("detail.do")
    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        String id = form.getString("id");
        String webNodeId = form.getString("webNodeId");

        if (Validate.check(Validate.OBJECTID, webNodeId) && (id == null || Validate.check(Validate.OBJECTID, id))) {
            WebContent webContent = webContentService.getDetail(id, webNodeId);

            if (webContent.isEmpty()) {
                LOGGER.error("所查看的栏目内容不存在");
                response.sendError(404);
                return null;
            }

            List<Cluster> webNodeOptions = webContentService.getOptions(webNodeId);

            modelMap.put("webContent", webContent);
            modelMap.put("webNodeOptions", webNodeOptions);
            modelMap.put("check", ClusterUtil.getCheck(WebContent.class));
        } else {
            LOGGER.error("错误参数：webNodeId=" + webNodeId + ",id=" + id);
            response.sendError(404);
            return null;
        }

        return new ModelAndView(URI + "/detail", modelMap);
    }


    @Privilege(module = "用户", function = "组管理", operation = "添加")
    @RequestMapping("add.do")
    public ModelAndView add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        String webNodeId = request.getParameter("webNodeId");
        List<Cluster> webNodeOptions = new ArrayList();

        if (Validate.check(Validate.OBJECTID, webNodeId)) {
            webNodeOptions = webContentService.getOptions(webNodeId);
        }

        modelMap.put("webNodeOptions", webNodeOptions);
        modelMap.put("webNodeId", webNodeId);
        modelMap.put("check", ClusterUtil.getCheck(WebContent.class));
        return new ModelAndView(URI + "/add", modelMap);
    }

    @Privilege(module = "用户", function = "组管理", operation = "添加")
    @RequestMapping("save.do")
    public void save(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        WebContent webContent = new WebContent();
        boolean result = form.getCluster(webContent);
        LOGGER.debug("添加组：" + webContent);

        if (result) {
            int code = webContentService.save(webContent);
            ResponseInfo responseInfo = new ResponseInfo(code);
            responseInfo.put("id", webContent.getId());
            out(response, responseInfo);
        } else {
            response.sendError(404);
            LOGGER.error("非法添加组：" + webContent);
        }
    }

    @Privilege(module = "栏目", function = "栏目内容", operation = "删除")
    @RequestMapping("remove.do")
    public void remove(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        List<String> ids = form.getList("id");
        LOGGER.debug("删除栏目内容：id=" + ids);

        if (Validate.check(Validate.OBJECTID, ids)) {
            int code = webContentService.remove(ids);
            out(response, code);
        } else {
            LOGGER.error("非法删除栏目内容：id=" + ids);
            response.sendError(404);
        }
    }
}
